﻿using AG.Apps.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace AG.Apps.WebServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    [AspNetCompatibilityRequirements(RequirementsMode =
     AspNetCompatibilityRequirementsMode.Allowed)]
    public class AdRotatorService : IAdRotatorService
    {
        public List<Ad> GetAds(string appCode, string language)
        {
            AGAppsEntities db = new AGAppsEntities();

            App app = db.Apps.SingleOrDefault(a => a.AppCode == appCode);
            if (app != null)
            {
                var qry = from a in app.AppAds
                          where a.Location ==1 && (a.Ad.Languages == "" || a.Ad.Languages.IndexOf(language.ToLower()) >= 0)
                          select a.Ad;

                return qry.ToList<Ad>();
            }

            return new List<Ad>();
        }

        public List<Ad> GetAdsByLocation(string appCode, string language, int location)
        {
            AGAppsEntities db = new AGAppsEntities();

            App app = db.Apps.SingleOrDefault(a => a.AppCode == appCode);
            if (app != null)
            {
                var qry = from a in app.AppAds
                          where
                            a.Location == location &&
                            (a.Ad.Languages == "" || a.Ad.Languages.IndexOf(language.ToLower()) >= 0)
                          select a.Ad;

                return qry.ToList<Ad>();
            }

            return new List<Ad>();
        }

        /* Test URL SPFE
        //apps.arthurgraus.nl/Services/AdRotatorService.svc/GetAds/SPFE/en-US?u=info%40arthurgraus.nl&h=https%3A%2F%2Farthurgraus.sharepoint.com%2Fsites%2FDeveloper&p=https%3A%2F%2Farthurgraus-f433a313a2ad95.sharepoint.com%2Fsites%2FDeveloper%2FAdRotator
        */
         public List<Ad> GetAdsWithAnalytics(string appCode, string language, string userName, string hostWebUrl, string appPage)
        {


            AGAppsEntities db = new AGAppsEntities();

            App app = db.Apps.SingleOrDefault(a => a.AppCode == appCode);
            if (app != null)
            {
                AppAnalytics aa = new AppAnalytics();

                aa.App = app;
                aa.AppPage = appPage;
                aa.HostWebUrl = hostWebUrl;
                aa.Username = userName;
                aa.StartDateTime = DateTime.Now;
                aa.Language = language;

                db.AppAnalytics.Add(aa);
                db.SaveChanges();

                var qry = from a in app.AppAds
                          where a.Location==1 && ( a.Ad.Languages == "" || a.Ad.Languages.IndexOf(language.ToLower()) >= 0)
                          select a.Ad;

                return qry.ToList<Ad>();
            }

            return new List<Ad>();
        }
        
        public void SetAnalytics(string appCode, string language, string userName, string hostWebUrl, string appPage)
        {
            AGAppsEntities db = new AGAppsEntities();

            App app = db.Apps.SingleOrDefault(a => a.AppCode == appCode);
            if (app != null)
            {
                AppAnalytics aa = new AppAnalytics();

                aa.App = app;
                aa.AppPage = appPage;
                aa.HostWebUrl = hostWebUrl;
                aa.Username = userName;
                aa.StartDateTime = DateTime.Now;
                aa.Language = language;

                db.AppAnalytics.Add(aa);
                db.SaveChanges();


            }
        }
    }
}
